<?php
error_reporting(E_ALL);

/**
 * 
 * @class agenda Class
 * @author Netiva 
 * @version 0.2
 */
class agenda {

	public $html='';            // buffer HTML
	public $monthStart='';  		// mois de debut
	public $monthEnd='';  			// mois de fin
	private $periodes=array();  // array des périodes
	private $events=array();    // array des evennements

	public function __construct() {
    $this->monthStart = date('Y').'-01';
		$this->monthEnd = date('Y').'-12';
	}

	public function set_interval($monthStart, $monthEnd=null) {  
    $this->monthStart = $monthStart;
		$this->monthEnd = ($monthEnd==null) ? $monthStart : $monthEnd;
	}

	public function add_periode($interval, $label, $attribute=array()) {
		$this->periodes[] = array($interval, $label, $attribute);
	}

	public function add_event($date, $label, $attribute=array()) {
    $this->events[] = array($date, $label, $attribute);
	}

	public function create() {
		$this->html = '';
    $monthArr = self::get_month_between($this->monthStart, $this->monthEnd);
		foreach($monthArr as $month) {
      $this->get_month_html($month);
    }
		return '<div class="agenda">'.$this->html.'</div>';
	}

	private function is_periode($date) {
		// on parcours les periodes
		foreach($this->periodes as $key => $periode) {
			// si += que le debut et -= que la fin
			if ((strtotime($date)-strtotime($periode[0][0]) >= 0) && (strtotime($date)-strtotime($periode[0][1]) <= 0) )
			return $key;
		}
		return false;
	}

	private function is_event($date) {
		foreach($this->events as $key => $event) {
      if ($date == $event[0])
      return $key;
		}
		return false;
	}

  private function get_monthname_by_monthnum($mois) {
	  $tableau = Array('', 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Aôut', 'Septembre', 'Octobre', 'Novembre', 'Décembre');
	  return (intval($mois) > 0 && intval($mois) < 13) ? $tableau[intval($mois)] : "Indéfini";
  }

  function get_month_between($start, $end) {
    list($as, $ms) = explode('-', $start);
    list($ae, $me) = explode('-', $end);

    $tAs = $as;
    $tAe = $ae;

    $tMs = $ms;
    $tMe = $me;

    for (; $as <= $ae; $ms++) {
        if ($ms == 13) {
            $ms = 1;
            $as++;
        }
        $stack[] =  $as . '-' . $ms;
        if ($ms == $tMe && $as == $tAe)
            break;
    }
    return $stack;
  }

	private function get_month_html($date) {

    $month = explode('-', $date);
    $year = $month[0];
    $month = $month[1];
    
    $thisMonthClass = ($month == date('m')) ? 'thisMonth' : '';
    $this->html.= '<div class="agenda-month '.$thisMonthClass.'">';
    $this->html.= '<div class="label">&bull; '.self::get_monthname_by_monthnum($month).' '.$year.'</div>';
    $this->html.= '<ul class="libelle"><li>L</li><li>M</li><li>M</li><li>J</li><li>V</li><li>S</li><li>D</li></ul>';

    $tableau = Array("0", "1", "2", "3", "4", "5", "6", "0");
    $nb_jour = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $pas = 0;
    $index = 1;

		// on boucle sur le nombre de jours pour ce mois
		while ($pas < $nb_jour) {

					if ($index == 1) $this->html.= '<ul>';

          // Si le jour de la boucle est egal au jour de la semaine en cours
          if (Date("w", mktime(0, 0, 0, $month, 1 + $pas, $year)) == $tableau[$index]) {

              // numéro du jour et date du jour
              $day = Date("j", mktime(0, 0, 0, $month, 1 + $pas, $year));
              $dateDay = Date("Y-m-d", mktime(0, 0, 0, $month, 1 + $pas, $year));

							$html = $day;
              $attribute = array();

              // CASE AUJOURD'HUI
              if ($dateDay == Date("Y-m-d")) {
                    $attribute['class'] = 'today';
              }

              // AUTRES CASE
              else {

                    //si periode
										$event = self::is_event($dateDay);
                    if ($event==false) 
                        $periode = self::is_periode($dateDay);

										// si event
                    if ($event!==false) {
                      $attribute = array_merge($attribute, $this->events[$event][2]);
                      $html = ($this->events[$event][1]!=='') ? $this->events[$event][1] : $day;
                    }
                    
                    // si periode
										else if ($periode!==false) {
                    	$attribute = array_merge($attribute, $this->periodes[$periode][2]);
                    }

              }

              // création de la case
              $li = '<li';
              foreach($attribute as $key => $val) {
                $li.= ' '.$key.'="'.$val.'"';
              }
              $li.= '>'.trim($html).'</li>';
              $this->html.= $li;

							$pas++;
          }
          
          else {
              // Ajout d'une case vide
              $this->html.= '<li class="vide">&nbsp;</li>';
          }
          
          if ($index == 7 && $pas < $nb_jour) {
						$this->html.= '</ul>';
						$index = 1;
					}else{
						$index++;
					}
    }

    // Ajustement des cases
    for ($i = $index; $i <= 7; $i++) {
      $this->html .= '<li class="vide">&nbsp;</li>';
    }
    $this->html .= '</div>';
	}

// fin.
}

/*
<style type="text/css">
div.agenda {}
div.agenda .agenda-month {float:left;height:174px;margin:0 5px 5px 0px;padding:5px;border:1px solid #ECEFF5;}
div.agenda .agenda-month .label {padding:0 5px;background:#9CB135;color:#FFF;}
div.agenda .agenda-month ul {margin:0px;padding:0px;border:0px;}
div.agenda .agenda-month ul li {display:inline-block;margin:0px;padding:0px;width:16px;height:20px;text-align:center;background:#ECEFF5;border:0px;cursor:hand;}
div.agenda .agenda-month ul li:hover {background:#D8E39F;}
div.agenda .agenda-month li.vide {background:#ECEFF5;}
div.agenda .agenda-month li.vide:hover {background:#ECEFF5;}
div.agenda .agenda-month li.today {background:#F4CCD3;}
div.agenda .agenda-month ul.libelle li {background:#D8E39F;color:#9CB135;}
div.agenda .thisMonth {border:1px solid #9CB135;}
</style>

$agenda = new agenda();
//$agenda->set_interval('2009-03');
$agenda->set_interval('2009-01', '2012-12');
$agenda->add_periode(array('2009-01-15', '2009-02-15'), 'periode 1', array('style'=>'background:#FFFF00;', 'onClick'=>"alert('Hello periode !');"));
$agenda->add_event('2009-02-10', '', array('style'=>'background:#FF0000;', 'onClick'=>"alert('Hello event !');"));
$agenda->add_event('2009-02-26', '<img src="http://kitripstatic.yolette.fr/images/sections/annonces-manager/icon-vote.png" alt="event" />', array('style'=>'background:#00FF55;', 'onClick'=>"alert('Hello event !');"));
echo $agenda->create();
*/

?>